package com.lvtu.monitor.module.web.exception;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.lvtu.monitor.shiro.UserUtils;
import org.apache.log4j.Logger;
import org.elasticsearch.common.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvmama.com.lvtu.common.utils.MemcachedUtil;
import com.lvtu.monitor.dao.exmatch.po.ExJobPattern;
import com.lvtu.monitor.dao.exmatch.po.ExPattern;
import com.lvtu.monitor.module.web.module.ResultHandler;
import com.lvtu.monitor.mongo.service.IBaseMongoService;
import com.lvtu.monitor.utils.Constants;

/**
 * @Title: ExPatternAction.java
 * @Package com.lvtu.monitor.module.web.exception
 * @Description: TODO
 * @author dengcheng
 * @date 2015-5-21 下午2:47:42
 * @version V1.0.0
 */
@Controller
// 在类上面定义，表明该类为控制器，返回字符串与redirect:xxx
@RequestMapping("/pattern")
public class ExPatternAction {

	@Autowired
	IBaseMongoService mongoService;

	private final static Logger logger = Logger
			.getLogger(ExPatternAction.class);

	/**
	 * 进入实时报警规则列表页
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public String index() {
		return "exmonitor/ex_pattern";
	}

	/**
	 * 加载实时报警规则列表数据
	 * 
	 * @return
	 */
	@RequestMapping("/loadPattern")
	@ResponseBody
	public ResultHandler loadPattern() {

		List<ExPattern> result = mongoService.findAll(ExPattern.class);
		ResultHandler list = new ResultHandler();
		list.setRows(result);
		return list;
	}

	/**
	 * 添加一条实时报警规则
	 * @param exPattern
	 * @return
	 */
	@RequestMapping("/addPattern")
	@ResponseBody
	public boolean addPattern(ExPattern exPattern) {
		mongoService.insert(exPattern);
		MemcachedUtil.getInstance().remove(Constants.KEY_ALL_EX_PATTERNS);
		logger.info(String.format("Operator [%s]:exPattern inserted...%s",
				UserUtils.getUserName(), JSON.toJSONString(exPattern)));
		return true;
	}
	
	/**
	 * 修改一条实时报警规则
	 * @param exPattern
	 * @return
	 */
	@RequestMapping("/updatePattern")
	@ResponseBody
	public boolean updatePattern(ExPattern exPattern, @RequestParam("_id")String id) {
		Map<String, Object> update = this.buildUpdateMap(exPattern);
		mongoService.updateOne(id, update, ExPattern.class);
		MemcachedUtil.getInstance().remove(Constants.KEY_ALL_EX_PATTERNS);
		logger.info(String.format("Operator [%s]:exPattern updated...%s",
				UserUtils.getUserName(), JSON.toJSONString(exPattern)));
		return true;
	}

	/**
	 * 删除一条实时报警规则
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delPattern")
	@ResponseBody
	public boolean delPattern(String id) {
		mongoService.removeOne(id, ExPattern.class);
		MemcachedUtil.getInstance().remove(Constants.KEY_ALL_EX_PATTERNS);
		logger.info(String.format("Operator [%s]:exPattern deleted...id:%s",
				UserUtils.getUserName(), id));
		return true;
	}

	/**
	 * 进入定时报警规则列表页
	 * 
	 * @return
	 */
	@RequestMapping("/jobIndex")
	public String jobIndex() {
		return "exmonitor/ex_pattern_jobs";
	}

	/**
	 * 加载定时报警规则列表数据
	 * 
	 * @return
	 */
	@RequestMapping("/loadJobPattern")
	@ResponseBody
	public ResultHandler loadJobPattern() {

		List<ExJobPattern> result = mongoService.findAll(ExJobPattern.class);
		ResultHandler list = new ResultHandler();
		list.setRows(result);
		return list;
	}

	/**
	 * 添加一条定时报警规则
	 * 
	 * @param jobPattern 报警规则
	 * @return
	 */
	@RequestMapping("/addJobPattern")
	@ResponseBody
	public boolean addJobPattern(ExJobPattern jobPattern) {
		mongoService.insert(jobPattern);
		return true;
	}

	/**
	 * 删除一条定时报警规则
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delJobPattern")
	@ResponseBody
	public boolean delJobPattern(String id) {
		mongoService.removeOne(id, ExJobPattern.class);
		return true;
	}
	
	private Map<String, Object> buildUpdateMap(ExPattern exPattern) {
		
		Map<String, Object> update = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(exPattern.getPattern()))
			update.put("pattern", exPattern.getPattern());
		if (StringUtils.isNotEmpty(exPattern.getPatternName()))
			update.put("patternName", exPattern.getPatternName());
		if (StringUtils.isNotEmpty(exPattern.getEmail()))
			update.put("email", exPattern.getEmail());
		if (StringUtils.isNotEmpty(exPattern.getMobile()))
			update.put("mobile", exPattern.getMobile());
		if (StringUtils.isNotEmpty(exPattern.getType()))
			update.put("type", exPattern.getType());
		update.put("intervalMinutes", exPattern.getIntervalMinutes());
		update.put("valid", exPattern.isValid());
		
		return update;
	}

}
